/**
 * 定义saga文件
 */
import { delay } from "redux-saga";
import { put, takeLatest } from "redux-saga/effects";
import * as actions from "../actions";

// generator函数不用箭头定义
function* loginAsync (action) {
    const { username, password } = action.payload;
    // console.log(">>>>>", username, password)
    try{
        if(username && password){
            yield delay(1000); //模拟一个异步，替换成后端请求api即可。例如：使用yeild call(axios.get(url))
            yield put(actions.loginSucc({
                name: "zhangsan",
                age: 20,
                addr: 'chengdu'
            }));
        }
        if(!username || !password){
            yield put(actions.loginErr('用户名或密码错误！'));
        }

    } catch(error) {
        console.log(error);
    }
}

export default function* rootSaga() {
  yield [
      takeLatest(actions.REQ_LOGIN, loginAsync)
  ];
}
